package pl.wroc.uni.ii.evolution.servlets.charts.pages;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.JFreeChart;
import pl.wroc.uni.ii.evolution.chart.EvChartTools;
import pl.wroc.uni.ii.evolution.chart.EvGenesAverageValuesChart;
import pl.wroc.uni.ii.evolution.chart.EvGenesOriginChart;
import pl.wroc.uni.ii.evolution.chart.EvObjectiveFunctionValueMaxAvgMinChart;
import pl.wroc.uni.ii.evolution.distribution.statistics.persistency.EvPersistentStatisticServletStorage;
import pl.wroc.uni.ii.evolution.engine.operators.general.statistic.EvStatistic;
import pl.wroc.uni.ii.evolution.engine.operators.general.statistic.avggenesvalue.EvGenesAvgValueStatistic;
import pl.wroc.uni.ii.evolution.engine.operators.general.statistic.genesorigin.EvGenesOriginStatistic;
import pl.wroc.uni.ii.evolution.engine.operators.general.statistic.maxavgmin.EvObjectiveFunctionValueMaxAvgMinStatistic;
import pl.wroc.uni.ii.evolution.engine.operators.general.statistic.persistency.EvStatisticFilter;
import pl.wroc.uni.ii.evolution.servlets.databasesupport.communication.EvDBServletCommunicationImpl;


/**
 * Servlet generating chart as a JPEG image.
 * 
 * Receives it's attributes via GET parameters (embedded in URL).
 * 
 * @author Krzysztof Sroka, Donata Malecka
 */
public class ChartImage extends HttpServlet {

  private static final long serialVersionUID = -8494959209551063720L;


  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    
    Long task_id = null;
    Long cell_id = null;
    Long node_id = null;
    Long type = null;
    String url = null;
    

    try {
      task_id = Long.parseLong(request.getParameter("task_id"));
      cell_id = Long.parseLong(request.getParameter("cell_id"));
      node_id = Long.parseLong(request.getParameter("node_id"));
      type = Long.parseLong(request.getParameter("type"));
      url = (String) request.getSession().getAttribute("url");
   
    } catch (Exception e) { // return "Bad request" response

      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      System.out.println(e.getMessage());
      return;
    }
    

    EvDBServletCommunicationImpl comm = new EvDBServletCommunicationImpl(url, "");
    
   
    try {
      response.setStatus(HttpServletResponse.SC_OK);
      response.setContentType("image/jpeg");
      generateImage(comm, task_id, cell_id, node_id, type, response.getOutputStream());
      
    } catch (Exception e) {
      e.printStackTrace(System.out);
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    }
  }


  private void generateImage(EvDBServletCommunicationImpl comm, Long task_id, Long cell_id, Long node_id, Long type,
      ServletOutputStream outputStream) throws IOException {
   
   
    
    EvPersistentStatisticServletStorage storage
    = new EvPersistentStatisticServletStorage(task_id, cell_id, node_id, comm);
    
    EvStatistic[] stats = storage.getStatistics();
    
    JFreeChart chart = null;
    if (type == 1L) {
      stats = EvStatisticFilter.byClass(EvObjectiveFunctionValueMaxAvgMinStatistic.class, stats);
      chart = EvObjectiveFunctionValueMaxAvgMinChart.createJFreeChart(stats, true);
      EvChartTools.writeToStream(outputStream, EvChartTools.draw(chart, 500, 300));
    }
    if (type == 2L) {
      stats = EvStatisticFilter.byClass(EvGenesAvgValueStatistic.class, stats);
      chart = EvGenesAverageValuesChart.createJFreeChart(stats, true);
      EvChartTools.writeToStream(outputStream, EvChartTools.draw(chart, 500, 300));
    }
    if (type == 3L) {
      stats = EvStatisticFilter.byClass(EvGenesOriginStatistic.class, stats);
      chart = EvGenesOriginChart.createJFreeChart(stats);
      EvChartTools.writeToStream(outputStream, EvChartTools.draw(chart, 500, 300));
    }
   
  }
}
